package com.xlf.common.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.xlf.cache.RoleList;
import com.xlf.common.model.Factory;
import com.xlf.common.model.User;
import com.xlf.web.SessionKey;

public class UserController extends Controller {
	public void login() {
		renderJsp("login.jsp");
	}

	public void loginForm() {
		String username = getPara("username");
		String password = getPara("pwd");
		String from = getPara("from");

		try {
			if (from != null && "factory".equals(from)) {
				Factory factory = Factory.dao.exeFindByName(username, password);
				if (factory != null) {
					int f_id = factory.get("f_id");
					// by zyk 把厂家管理员的fid放入session中
					setSessionAttr("f_id", f_id);
					saveFactory(factory);
					forwardAction("/factory/ordersearch");
				} else { // 无此用户
					backtoFrom(from, "登陆账号验证失败，非有效用户");
				}

			} else {
				User user = User.dao.exeFindByName(username, password);
				if (user != null) {
					int status = user.getInt("status");
					int type = user.getInt("type");
					if (status == 0) {
						// 用户已经作废
						backtoFrom(from, "用户已被冻结，禁止登陆");
					} else {
						if (type == 10 || type == 11) {
							if(!from.equals("material")) {
								backtoFrom(from, "用户账号类型不匹配");
							} else {
								// 主材部人员登陆
								saveUser(user);
								forwardAction("/order/index");
							}
						} else if (type == 20 || type == 21) {
							if(!from.equals("designer")) {
								backtoFrom(from, "用户账号类型不匹配");
							} else {
								// 设计部人员登陆
								saveUser(user);
								forwardAction("/designer/plan");
							}
						} else {
							// 用户类型不识别
							backtoFrom(from, "用户类型错误(type=" + type + ")");
						}
					}
				} else { // 无此用户
					backtoFrom(from, "登陆账号验证失败，非有效用户");
				}
			}
		} catch (Exception e) {
			backtoFrom(from, "登陆账号验证失败，非有效用户");
		}
	}

	public void logout() {
		removeSessionAttr(SessionKey.USER_TYPE);
		removeSessionAttr(SessionKey.USER);

		render("/index.html");
		
		/*
		String from = getPara("from");
		if (from.equals("designer")) {
			forwardAction("/designer/login");
		} else if (from.equals("material")) {
			forwardAction("/user/login");
		}else if (from.equals("factory")) {
			forwardAction("/factory/login");
		}
		*/
	}

	private void saveUser(User user) {
		setSessionAttr(SessionKey.USER, user);
		setSessionAttr(SessionKey.USER_TYPE, user.getInt("type"));
	}

	private void saveFactory(Factory user) {
		setSessionAttr(SessionKey.USER, user);
		setSessionAttr(SessionKey.USER_TYPE, user.getInt("type"));
	}

	private void backtoFrom(String from, String notice) {
		removeSessionAttr(SessionKey.USER);
		removeSessionAttr(SessionKey.USER_TYPE);

		setAttr("notice", notice);

		if (from.equals("designer")) {
			forwardAction("/designer/login");
		} else if (from.equals("material")) {
			forwardAction("/user/login");
		}else if (from.equals("factory")) {
			forwardAction("/factory/login");
		}
	}

	public void index() {
		String name = getPara("name");
		String roleTypeStr = getPara("roleType");

		if (name != null) {
			setAttr("name", name);
		}
		int roleType = -1;
		if (roleTypeStr != null) {
			roleType = Integer.parseInt(roleTypeStr);
		}
		setAttr("roleType", roleType);
		setAttr("roleList", RoleList.getList());

		Page<User> pageRecord = User.dao.exeSearchSql(getParaToInt("page", 1), 20, name, roleType);
		setAttr("pageRecord", pageRecord);
		renderJsp("index.jsp");
	}

	public void save() {
		User user = getModel(User.class, "user");
		if (user.get("u_id") == null) {
			user.set("status", "1");
			user.save();
		} else if (user.getInt("u_id") > 0) {
			user.update();
		}
		index();
	}

	public void form() {
		Integer id = getParaToInt("id");
		if (id != null && id > 0) {
			setAttr("user", User.dao.findById(id));
		}
		setAttr("roleList", RoleList.getList());
		renderJsp("form.jsp");
	}

	public void hold() {
		Integer id = getParaToInt("id");
		User user = User.dao.findById(id);
		if (user != null) {
			user.set("status", "0");
			user.update();
		}
		index();
	}

	public void recover() {
		Integer id = getParaToInt("id");
		User user = User.dao.findById(id);
		if (user != null) {
			user.set("status", "1");
			user.update();
		}
		index();
	}

	public void addNew() {
		setAttr("roleList", RoleList.getList());
		renderJsp("form.jsp");
	}

}
